package com.pengcheng.taskapplication.wxapi;


import com.google.gson.Gson;
import com.pengcheng.taskapplication.R;
import com.pengcheng.taskapplication.contants.ConstantValue;
import com.pengcheng.taskapplication.model.PayTypeModel;
import com.pengcheng.taskapplication.mvp.presenter.UserPresenterV2;
import com.pengcheng.taskapplication.mvp.view.activity.MyParkAndWashList;
import com.pengcheng.taskapplication.mvp.view.activity.MyParkList;
import com.pengcheng.taskapplication.mvp.view.activity.ParkWashStatus;
import com.pengcheng.taskapplication.mvp.view.activity.ParkWashStatusV2;
import com.pengcheng.taskapplication.net.IView;
import com.pengcheng.taskapplication.util.SPUtils;
import com.pengcheng.taskapplication.util.ToastUtils;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelpay.PayResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler, IView {

    private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";
    private UserPresenterV2 userPresenter;
    private IWXAPI api;

    TextView tv_center_title;
    ImageView img_back;
    LinearLayout ll_confirm;
    TextView tv_show;
    private String uid;
    private String newtoken;
    private String tradno="";
    private String deviceid="";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay_success_layout);
        userPresenter = new UserPresenterV2(this);
        uid= SPUtils.getString(getApplicationContext(),ConstantValue.SpType.userid);
        newtoken=SPUtils.getString(getApplicationContext(),ConstantValue.SpType.newtoken);
        api = WXAPIFactory.createWXAPI(this, ConstantValue.APP_ID);
        api.handleIntent(getIntent(), this);
        tv_center_title = findViewById(R.id.tv_center_title);
        img_back = findViewById(R.id.img_back);

        ll_confirm = (LinearLayout) findViewById(R.id.ll_confirm);
        tv_center_title.setText("支付完成");
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                WXPayEntryActivity.this.finish();
            }
        });
        ll_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(tv_show.getText().toString().equals("支付失败")){
                    WXPayEntryActivity.this.finish();
                }else{

                }
            }
        });

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @Override
    public void onResp(BaseResp resp) {
        Log.d("msg", "onPayFinish, errCode = " + resp.errCode);
        tv_show=(TextView)findViewById(R.id.tv_show);
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            if (resp.errCode == 0) {

                PayResp payResp = (PayResp) resp;
                String channel = payResp.extData;
                PayTypeModel model = new Gson().fromJson(channel, PayTypeModel.class);
                //停车支付
                if (model.type.equals("park")) {
                    Intent intent = new Intent(this, MyParkAndWashList.class);
                    startActivity(intent);
                    WXPayEntryActivity.this.finish();
                } else if (model.type.equals("wash")) {
                    //洗车支付成功，跳转到洗车中状态
//                    Intent intent = new Intent(this, ParkWashStatus.class);
//                    intent.putExtra("tradno", model.getExtData());
//                    intent.putExtra("deviceid", model.getExtData2());
//                    startActivity(intent);
//                    WXPayEntryActivity.this.finish();
                    //支付成功后，判断下设备状态，可以开启的话，开启设备
                    tradno=model.getExtData();
                    deviceid=model.getExtData2();
                    startDevice(newtoken,uid,model.getExtData());

                }
            } else if (resp.errCode == -1) {
                tv_show.setText("支付失败");
                Toast.makeText(this, "支付异常,请重新支付或联系客服", Toast.LENGTH_SHORT).show();
            } else if (resp.errCode == -2) {
                Toast.makeText(this, "已取消支付", Toast.LENGTH_SHORT).show();
                tv_show.setText("支付失败");
            }else {
                tv_show.setText("支付失败");
            }
        }
    }

    @Override
    public void onSuccess(String code, String msg, Object data, String actionType) {
        if(actionType.equals(ConstantValue.RequestKey.startDevice)){
            if(code.equals("0")){
                //开启成功话，进入到轮询页面进行轮询
                Intent intent =new Intent(getApplicationContext(), ParkWashStatusV2.class);
                intent.putExtra("tradno",tradno);
                intent.putExtra("deviceid",deviceid);
                startActivity(intent);
                WXPayEntryActivity.this.finish();
            }else{
                //提示为什么开启失败
                ToastUtils.showShortToast(msg);
            }

        }
    }

    @Override
    public void onFailed(String code, String msg) {

    }

    public void startDevice(String token,String uid,String tradno){
        userPresenter.startDevice(token,uid,tradno, ConstantValue.RequestKey.startDevice);
    }
}